package studio.raptor.ddal.dashboard.log.thread;

import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.dashboard.log.bo.LogMessageObject;
import studio.raptor.ddal.dashboard.log.common.CommonMethodUtil;
import studio.raptor.ddal.dashboard.log.common.LogServerConstant;
import studio.raptor.ddal.dashboard.log.queue.interfaces.IMessageQueue;
import studio.raptor.ddal.dashboard.log.service.interfaces.ILogStorageService;
/**
 * 类名称<br>
 * 这里是类的描述区域，概括出该的主要功能或者类的使用范围、注意事项等
 * <hr>
 * 修改记录
 * <hr>
 * 1、修改人员:    修改时间:<br>       
 *    修改内容:
 * <hr>
 */
@Getter
@Setter
public class FrontQueueThread implements Runnable{

	private static Logger log = LoggerFactory.getLogger( FrontQueueThread.class);

	private boolean flag = true;
//	private String databaseType;
	private IMessageQueue messageQueue;
	private ILogStorageService logStorageService;

	public FrontQueueThread(ILogStorageService logStorageService,IMessageQueue messageQueue){
		this.logStorageService=logStorageService;
		this.messageQueue=messageQueue;
	}

//	public FrontQueueThread(){
//		if(this.logStorageService==null){
//			ApplicationContext context = new AnnotationConfigApplicationContext(LogConfiguration.class);
//			this.logStorageService =(ILogStorageService)context.getBean("LogStorageMysqlService");
//		}
//	}

	public void run() {
		while(flag){
			try {
				log.debug(Thread.currentThread().getId()+" queue length is "+messageQueue.getLength());
				if(messageQueue.getLength()>0){
					LogMessageObject logMessageObject = (LogMessageObject)messageQueue.readObject();
					logStorageService.breakUpAndDBStorage(logMessageObject);
				}else{
					log.debug( "队列为空，暂时休眠" );
					CommonMethodUtil.waitSleep( LogServerConstant.LOG_SERVER_FRONTQUEUETHREAD_SLEEP_TIME);
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
